package com.chenqi.controller;

import com.chenqi.model.dto.PostDTO;
import com.chenqi.model.entity.PageInfo;
import com.chenqi.model.entity.Post;
import com.chenqi.service.PostService;
import com.chenqi.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @program: ardaicommunity
 * @author: ChenQi
 * @create: 2021-03-2021/3/11-14-42
 */
@RestController
@RequestMapping("/api/v1/post")
public class PostController {

    @Autowired
    private PostService postService;

    /**
     * 分页查询
     * @param sectionId
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("/list/{sectionId}/{page}/{rows}")
    public JsonData list(@PathVariable(value = "sectionId",required = true) Integer sectionId,
                         @PathVariable(value = "page",required = false) Integer page,
                         @PathVariable(value = "rows",required = false) Integer rows) {
        //对于分页信息进行预处理
        page = page == null ? 1 : page;
        rows = rows == null ? 6 : rows;
        //总条数
        Integer counts = postService.counts(sectionId);
        //总数据
        List<Post> postByPage = postService.getPostByPage(page, rows, sectionId);
        //总页数
        Integer totalPages = counts % rows == 0 ? counts / rows : counts / rows + 1;
        List<Integer> integerList = new ArrayList<>();
        for (int i = 1; i <= totalPages; i++) {
            integerList.add(i);
        }
        //封装分页信息
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPage(page).setRows(rows).setSectionId(sectionId).setCounts(counts).setPostList(postByPage).setTotalPages(totalPages).setPageList(integerList);
        return JsonData.BuildSuccess(pageInfo,"查询成功!");
    }

    /**
     * 添加帖子
     * @param postDTO
     * @return
     */
    @PostMapping("/addPost")
    public JsonData addPost(@Valid @RequestBody PostDTO postDTO) {
        int i = postService.addPost(postDTO);
        return i == 0 ? JsonData.BuildError("新增失败") : JsonData.BuildSuccess("新增成功");
    }

    /**
     * 文章详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public JsonData detail(@PathVariable("id") Integer id) {
        Post postById = postService.getPostById(id);
        return postById == null ? JsonData.BuildError("获取详情失败") : JsonData.BuildSuccess(postById, "获取成功!");
    }


    /**
     * 获取推荐帖子
     * @return
     */
    @GetMapping("/recommend")
    public JsonData recommend(){
        List<Post> recommends = postService.recommend();
        return recommends == null ? JsonData.BuildError("查询失败") : JsonData.BuildSuccess(recommends, "查询成功!");
    }

    /**
     * 删除帖子
     * @param pid
     * @return
     */
    @DeleteMapping("/remove/{pid}")
    public JsonData remove(@PathVariable("pid") Integer pid) {
        int i = postService.removePost(pid);
        return i == 0 ? JsonData.BuildError("删除失败") : JsonData.BuildSuccess("删除成功");
    }


    /**
     * 根据标题进行模糊查询
     * @param title
     * @return
     */
    @GetMapping("/search/{title}")
    public JsonData search(@PathVariable("title") String title) {
        List<Post> search = postService.search(title);
        return search == null ? JsonData.BuildError("查询失败") : JsonData.BuildSuccess(search, "查询成功");
    }

    @GetMapping("/userPost/{id}")
    public JsonData userPost(@PathVariable("id") Integer id) {
        List<Post> postByUserId = postService.getPostByUserId(id);
        return postByUserId == null ? JsonData.BuildError("查询失败") : JsonData.BuildSuccess(postByUserId, "查询成功");
    }

}
